package com.nikkerball.apps.darkhorse.model.security;

import com.nikkerball.apps.darkhorse.model.base.Person;
import com.nikkerball.apps.darkhorse.model.change.ChangeEvent;
import com.nikkerball.apps.darkhorse.model.change.ChangeValue;
import com.nikkerball.apps.darkhorse.model.core.ClassYear;

import javax.persistence.*;
import java.util.List;

/**
 * Represents a User
 */
@Entity
@Table(name = "User",
        uniqueConstraints = {@UniqueConstraint(columnNames = {"userName"}),
                             @UniqueConstraint(columnNames = {"email"})})
public class User extends Person {

    // ----- class fields -----

    private static final int EMAIL_COLUMN_SIZE = 200;

    private static final int PASSWORD_COLUMN_SIZE = 15;

    private static final int PIN_COLUMN_SIZE = 6;

    private static final int USER_NAME_COLUMN_SIZE = 15;

    // ----- instance fields -----

    private List<ClassYear> classYears;

    private String email;

    private String password;

    private int pinCode;

    private String userName;

    // ----- constructors -----

    public User() {
        super();
    }

    public User(String name, String surname, String userName, String email, String password) {
        super(name, surname);
        this.userName = userName;
        this.email = email;
        this.password = password;
    }

    // ----- public methods -----

    @Transient
    public List<ChangeValue> getChangeValues(ChangeEvent changeEvent) {
        List<ChangeValue> changeValues = super.getChangeValues(changeEvent);
        changeValues.add(new ChangeValue("email", getEmail(), changeEvent));
        return changeValues;
    }

    @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    @JoinTable(name = "ClassYear_User", joinColumns = @JoinColumn(name = "UserId"),
            inverseJoinColumns = @JoinColumn(name = "ClassYearId"))
    public List<ClassYear> getClassYears() {
        return this.classYears;
    }

    public void setClassYears(List<ClassYear> classYears) {
        this.classYears = classYears;
    }

    @Column(length = EMAIL_COLUMN_SIZE, nullable = false)
    public String getEmail() {
        return this.email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Column(length = PASSWORD_COLUMN_SIZE, nullable = false)
    public String getPassword() {
        return this.password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Column(length = PIN_COLUMN_SIZE)
    public int getPinCode() {
        return this.pinCode;
    }

    public void setPinCode(int pinCode) {
        this.pinCode = pinCode;
    }

    @Column(length = USER_NAME_COLUMN_SIZE, nullable = false)
    public String getUserName() {
        return this.userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
}
